package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.TransferTransaction;


@Repository
public class TransferTransactionDAO {
	
	@PersistenceContext private EntityManager em;

	 @Transactional
	public List<TransferTransaction> getTransferTransactions() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * Add an transfer transaction
	 * @param transaction
	 * @return
	 */
	 @Transactional
	public boolean addTransferTransation(TransferTransaction transaction) {
//		EntityManager em = TransactionManager.getEntityManager();
		// Check if there is enough money for transfer? (Here or on jsp?)
		em.persist(transaction);
		return true;
	}

	/**
	 * Get all transfer transactions based on source account's id
	 * @param id
	 * @return
	 */
	 @Transactional
	public List<TransferTransaction> getTransferTransactionBySourceId(Integer id) {
//		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<TransferTransaction> query = null;
		try {
			query = em.createQuery("SELECT a FROM TransferTransaction a WHERE srcAccountNum = :srcNum",
					TransferTransaction.class).setParameter("srcNum", id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Remember to check null later
		return query.getResultList();
	}

}
